<?php
namespace Home\Controller;
use Think\Controller;
class BlogsController extends CommonController {

	public function bloginfo(){

		//获取到问题的ID
		$questionId = $_GET['qid'];

		//浏览量自增
		$this->autoIncrement('question','view',$questionId);

		$result = M('question')->find($_GET['qid']);
		//得到对应的标签名字
        $cateTagSql = "select t1.cname from category t1 RIGHT  JOIN category_question t2 on t1.id = t2.cid LEFT JOIN question t3 ON t3.id = t2.qid WHERE t2.qid = {$_GET['qid']}";
        $cateTagArr = M()->query($cateTagSql);

        //得到提问者的名字
        $mansql = "select user.username from user,question where user.id = question.uid order by create_time desc limit 1";
        $result['username'] = M()->query($mansql)[0]['username'];
        
        //获得回答的答案及回答的人的名字
        // $answersql = "select * from answer left join user on user.id = answer.uid where qid = {$_GET['qid']}";
        // $answersql = "select user.id as userid,user.username,answer.id as answerid,answer.fid,answer.qid,answer.uid from answer left join user on user.id = answer.uid where qid = {$_GET['qid']}";
        $answersql = "select user.id as userid,user.username,user.face,answer.* from answer left join user on user.id = answer.uid where qid = {$_GET['qid']}";
        $answerResult = M()->query($answersql);

        $this->assign('answerResult',$answerResult);
        $this->assign('cateTagArr',$cateTagArr);
		$this->assign('question',$result);

		$this->display();
	}

	//what the fuck 点赞 贬低功能
	public function btnClick(){
		$theType = $_POST['thetype'];            //得到点击过来的是问题的还是答案的
		$theAction = $_POST['theaction'];		 //得到的是喜欢的还是讨厌的 like || hate
		$thedataid = $_POST['theid'];				 //获取post过来的ID
		//如果过来的是赞 执行like的方法 如果是贬低的 执行hate方法
		$this->ajaxReturn($this->$theAction($theType,$thedataid));
	}

	public function hate($table,$dataid){

		//判断参数table是question的还是answer的 是answer的话answer_vote也要插
		// 是quetion的话 question_vote也要插入数据
		if( $table == 'question' ){
			$sql = "select * from question_vote where uid = {$_SESSION['uid']} and qid = {$dataid}";
			//插入点赞表里是否有当前点击的用户的信息 有的话说明已经投票了 不能继续投票
			if(M()->query($sql)){
				$returnMsg = array(
					'error'    => '1',
					'msg'      => '已经投过票啦',
				);
			//否则就是还没有贬低 
			}else{
				$sql = "insert into question_vote(qid,uid,create_time,vote_type) values({$dataid},{$_SESSION['uid']},".time().",'-1')";
				//没有点赞插入成功之后 还要更新一下quetion表的 _vote 字段
				if(M()->execute($sql)){

					if($this->autoDown($table,'vote_num',$dataid)){
						$returnMsg = array(
							'error' => '0',
							'msg'   => '贬低成功',
							'vote_num' => M('question')->where("id = {$dataid}")->getField('vote_num'),
						);
					}else{
						$returnMsg = array(
							'error' => '1',
							'msg'   => '贬低出错',
						);
					}
				}
			}
		}else{

			$sql = "select * from answer_vote where uid = {$_SESSION['uid']} and qid = {$dataid}";
			//插入投票表里是否有当前点击的用户的信息 有的话说明已经投票了 不能继续投票
			if(M()->query($sql)){
				$returnMsg = array(
					'error' => '1',
					'msg'   => '已经投过票啦',
				);
			//否则就是还没有贬低 
			}else{
				$sql = "insert into answer_vote(qid,uid,create_time,vote_type) values({$dataid},{$_SESSION['uid']},".time().",'-1')";
				//没有点赞插入成功之后 还要更新一下quetion表的 quetion_vote 字段
				if(M()->execute($sql)){
					if($this->autoDown($table,'vote_num',$dataid)){
						$returnMsg = array(
							'error' => '0',
							'msg'   => '贬低成功',
							'vote_num' => M('answer')->where("id = {$dataid}")->getField('vote_num'),
						);
					}else{
						$returnMsg = array(
							'error' => '1',
							'msg'   => '贬低出错',
						);
					}
				}
			}
		}
		
		return $returnMsg;
	}

	public function like($table,$dataid){
		//判断参数table是question的还是answer的 是answer的话answer_vote也要插
		// 是quetion的话 question_vote也要插入数据
		if( $table == 'question' ){
			$sql = "select * from question_vote where uid = {$_SESSION['uid']} and qid = {$dataid}";
			//插入点赞表里是否有当前点击的用户的信息 有的话说明已经投过票 不能继续点赞
			if(M()->query($sql)){
				$returnMsg = array(
					'error' => '1',
					'msg'   => '已经透过票啦',
				);
			//否则就是还没有赞美 
			}else{
				$sql = "insert into question_vote(qid,uid,create_time,vote_type) values({$dataid},{$_SESSION['uid']},".time().",'+1')";
				//没有点赞插入成功之后 还要更新一下quetion表的 quetion_vote 字段
				if(M()->execute($sql)){

					if($this->autoIncrement($table,'vote_num',$dataid)){
						$returnMsg = array(
							'error'    => '0',
							'msg'      => '赞美成功',
							'vote_num' => M('question')->where("id = {$dataid}")->getField('vote_num'),
						);
					}else{
						$returnMsg = array(
							'error' => '1',
							'msg'   => '赞美出错',
						);
					}
				}
			}
		}else{
			$sql = "select * from answer_vote where uid = {$_SESSION['uid']} and qid = {$dataid}";
			//插入点赞表里是否有当前点击的用户的信息 有的话说明已经投过票 不能继续投过票
			if(M()->query($sql)){
				$returnMsg = array(
					'error' => '1',
					'msg'   => '已经投过票啦',
				);
			//否则就是还没有赞美 
			}else{
				$sql = "insert into answer_vote(qid,uid,create_time,vote_type) values({$dataid},{$_SESSION['uid']},".time().",'+1')";
				//没有点赞插入成功之后 还要更新一下quetion表的 _vote 字段
				if(M()->execute($sql)){
					if($this->autoIncrement($table,'vote_num',$dataid)){
						$returnMsg = array(
							'error' => '0',
							'msg'   => '赞美成功',
							'vote_num' => M('answer')->where("id = {$dataid}")->getField('vote_num'),
						);
					}else{
						$returnMsg = array(
							'error' => '1',
							'msg'   => '赞美出错',
						);
					}
				}
			}
		}

		return $returnMsg;
	}


	//提交回答的方法
	public function submitAnswer(){
		$data['qid'] = $_POST['qid'];
		$data['uid'] = $_SESSION['uid'];
		$data['content'] = $_POST['editorValue'];
		$data['create_time'] = time();

		// var_dump($data);
		if(M('answer')->add($data)){
			$this->success('回复成功',U('home/Blogs/bloginfo/',array('qid'=>$_POST['qid'])));
		}else{
			$this->error('回复失败');
		}
	}
}